﻿using System;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Account.Model;
using Billing.Model;
using Identity.IService.Entry;
using Account.IService.Entry;
using Billing.IService.Entry;
using Framework.MVVM.Model;
using Common.IService;
using Common.Service;
using Log.IService;
using Log.Service;
using Identity.IService;
using Identity.Service;
using Account.IService;
using Account.Service;
using Billing.IService;
using Billing.Service;
using Account.Model.Behavior;


namespace Recharge.Models
{
    [ServiceAttribute(typeof(IRechargeOrderService), typeof(RechargeOrderService), "Instance")]
    [ServiceAttribute(typeof(IRemoteCodeService), typeof(RemoteCodeService), "Instance")]
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class ReChargeAccountModel : AsyncModelBase, IWriteLog, IValidateIdentity, IValidateVerifyCode, IGetAccount, INewRechargeOrder
    {
        #region Field

        private const string m_empty_verifycode = "rechge000";
        private const string m_error_verifycode = "rechge001";
        private const string m_complete_verifycode = "rechge002";
        private const string m_error_verifyvalue = "rechge003";
        private const string m_complete_verifyvalue = "rechge004";
        private const string m_empty_identity = "rechge005";
        private const string m_error_identity = "rechge006";
        private const string m_complete_identity = "rechge007";
        private const string m_error_account_format = "rechge008";
        private const string m_error_account_not_exists = "rechge009";
        private const string m_error_service = "rechge010";
        private const string m_complete_getreceiver = "rechge011";
        private const string m_error_neworder_amount = "rechge012";
        private const string m_error_neworder = "rechge013";
        private const string m_complete_neworder = "rechge014";

        private const string m_resource_verify = "vc00";
        private const string m_resource_user = "u000";

        private const string m_entry = "entry";
        private const string m_error = "error";

        private const string m_errors_verifycode = "verifycode";
        private const string m_errors_identity = "identity";
        private const string m_errors_account = "account";
        private const string m_errors_order = "order";

        private const string m_split = ";";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateVerifyCode(m_resource_verify, m_empty_verifycode, m_error_verifycode, m_complete_verifycode, m_error_verifyvalue, m_complete_verifyvalue);

        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateVerifyCode

        public string VerifyCode { get; set; }

        public string Source { get; set; }

        public string VerifyValue { get; set; }

        public void OnValidateVerifyCodeComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);
            if (string.Equals(key, m_complete_verifyvalue))
            {
                this.ValidateIdentity(m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
            }

        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error, m_errors_verifycode);
            this.Log(key, new IdentityEntry { code = VerifyCode, source = Source, resource = m_resource_verify, value = VerifyValue });
        }

        #endregion IValidateVerifyCode

        #region IValidateIdentity

        public string IdentityCode { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            RechargeOrderOwner = entry.value;
            this.GetAccount(m_error_account_format, m_error_account_not_exists, m_error_service, m_complete_getreceiver);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete(m_error, m_errors_identity);
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

        #endregion IValidateIdentity

        #region IGetAccount

        public string AccountName { get; set; }

        public void OnGetAccountComplete(string key, AccountEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            RechargeOrderReceiver = entry.code;
            this.NewRechargeOrder(m_error_neworder_amount, m_error_neworder, m_complete_neworder);
        }

        public void OnGetAccountError(string key)
        {
            OnComplete(m_error, m_errors_account);
            this.Log(key, Source + m_split + AccountName);
        }

        #endregion IGetAccount

        #region INewRechargeOrder

        public int RechargeOrderAmount { get; set; }

        public string RechargeOrderOwner { get; set; }

        public string RechargeOrderReceiver { get; set; }

        public string RechargeOrderType { get; set; }

        public string RechargeOrderSource { get; set; }

        public void OnNewRechargeOrderComplete(string key, RechargeOrderEntry entry)
        {
            OnComplete(m_entry, entry);
            this.Log(key, entry);

        }

        public void OnNewRechargeOrderError(string key)
        {
            OnComplete(m_error, m_errors_order);
            this.Log(key, new RechargeOrderEntry { owner = RechargeOrderOwner, receiver = RechargeOrderReceiver, amount = RechargeOrderAmount, type = RechargeOrderType });
        }

        #endregion INewRechargeOrder
    }
}